/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mh.bol;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Properties;
import mh.dal.DAO;
import mh.entity.ComplaintEn;

/**
 *
 * @author VIETNH
 */
public class ComplaintBO {

    DAO dao = new DAO();

    public ArrayList<ComplaintEn> getComplaintDetailByCID(String cID) {
        ArrayList<ComplaintEn> listComp = new ArrayList<ComplaintEn>();
        Properties p = new Properties();
        p.put(1, cID);
        try {
            ResultSet rs = (ResultSet) dao.executeProc("sp_GetComplaintDetailByCID", p, "query");

            while (rs.next()) {
                ComplaintEn ce = new ComplaintEn();
                ce.setDescription(rs.getString("CD_Description"));
                ce.setTitle(rs.getString("CD_Title"));
                ce.setC_ID(rs.getString("C_ID"));
                ce.setE_ID(rs.getString("E_ID"));
                listComp.add(ce);
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return listComp;
    }

    public ArrayList<ComplaintEn> getComplaintByEID(String eID) {
        ArrayList<ComplaintEn> listComp = new ArrayList<ComplaintEn>();
        Properties p = new Properties();
        p.put(1, eID);
        try {
            ResultSet rs = (ResultSet) dao.executeProc("sp_GetComplaintByEID", p, "query");

            while (rs.next()) {
                ComplaintEn ce = new ComplaintEn();
                ce.setDatePost(rs.getString("C_DatePost"));
                ce.setTitle(rs.getString("C_Title"));
                ce.setC_ID(rs.getString("C_ID"));
                ce.setE_ID(rs.getString("E_ID"));
                ce.setActive(rs.getBoolean("C_Active"));
                ce.setStatus(rs.getBoolean("C_Status"));
                ce.setType(rs.getInt("C_Type"));
                ce.setDeadline(rs.getString("C_DeadLine"));
                listComp.add(ce);
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return listComp;
    }

    //Called when User or Staff reply the Complaint
    public boolean replyComplaint(ComplaintEn ce) {
        Properties p = new Properties();
        p.put(1, ce.getC_ID());
        p.put(2, ce.getE_ID());
        p.put(3, ce.getTitle());
        p.put(4, ce.getDescription());
        return (Boolean) dao.executeProc("sp_AddNewComplaintDetail", p, "update");
    }
    
    //Called when User post first
    public boolean addNewComplaint(ComplaintEn ce) {
        boolean flag = false;
        if (addComplaint(ce)) {
            if (addComplaintDetail(ce)) {
                flag = true;
            }
        }
        return flag;
    }

    public boolean addComplaint(ComplaintEn ce) {
        Properties p = new Properties();
        p.put(1, ce.getTitle());
        p.put(2, ce.getE_ID());
        p.put(3, ce.getType());
        p.put(4, ce.getDatePost());
        p.put(5, ce.getDeadline());
        p.put(6, ce.isStatus());
        p.put(7, ce.isActive());
        return (Boolean) dao.executeProc("sp_AddNewComplaint", p, "update");
    }

    public boolean addComplaintDetail(ComplaintEn ce) {
        Properties p = new Properties();

        int top = getTopEmPostByEID(ce.getE_ID());
        if(top == 0){
            top = 1;
        }
        ce.setC_ID(top + "");
        p.put(1, ce.getC_ID());
        p.put(2, ce.getE_ID());
        p.put(3, ce.getTitle());
        p.put(4, ce.getDescription());
        return (Boolean) dao.executeProc("sp_AddNewComplaintDetail", p, "update");
    }

    public int getTopEmPostByEID(String e_ID) {
        int count = 0;
        Properties p = new Properties();
        p.put(1, e_ID);
        ResultSet rs = (ResultSet) dao.executeProc("sp_GetTopEmPostByEID", p, "query");
        try {
            if (rs.next()) {
                count = rs.getInt(1);
                return count;
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
            return count;
        }
        return count;
    }
}
